import Vue from 'vue'
// 插件
import VueRouter from 'vue-router'

Vue.use(VueRouter);


const routes = [{
    path: '/',
    component: r => require.ensure([], () => r(require('../pages/index/index'))),
    meta:{
        need_login:true,
        title:"首页"
    }
},{
    path: '/login',
    component: r => require.ensure([], () => r(require('../pages/index/login'))),
    meta:{
        need_login:false,
        title:"登录"
    }
},{
    path: '/sets/index',
    component: r => require.ensure([], () => r(require('../pages/sets/index.vue'))),
    meta:{
        need_login:false,
        title:"设置"
    }
},{
    path: '/product/index',
    component: r => require.ensure([], () => r(require('../pages/product/index.vue'))),
    meta:{
        need_login:false,
        title:"商品管理"
    }
},{
    path: '/account/index',
    component: r => require.ensure([], () => r(require('../pages/account/index.vue'))),
    meta:{
        need_login:false,
        title:"账号管理"
    }
},{
    path: '/monitor/index',
    component: r => require.ensure([], () => r(require('../pages/monitor/index.vue'))),
    meta:{
        need_login:false,
        title:"监视"
    }
},{
    path: '/sell/index',
    component: r => require.ensure([], () => r(require('../pages/sell/index.vue'))),
    meta:{
        need_login:false,
        title:"收银"
    }
},{
    path: '/buy/index',
    component: r => require.ensure([], () => r(require('../pages/sell/index.vue'))),
    meta:{
        need_login:false,
        title:"出银"
    }
},{
    path: '/statistics/index',
    component: r => require.ensure([], () => r(require('../pages/statistics/index.vue'))),
    meta:{
        need_login:false,
        title:"统计"
    }
}];







const router = new VueRouter({
    mode:"hash",
    routes
})

router.beforeEach((to, from, next) => {
    // console.log("from",from)
    // console.log("to",to)

    if (to.meta.need_login && localStorage.userId == "") {
        Vue.prototype.err("需要登录");
        next('/login');
        return;
    }

    if (to.meta.title) {
        document.title = to.meta.title;
    }

    next();

})


router.afterEach( ( to, from, next ) => {

});

export default router;
